package com.qiyang.sell.controller;

import com.qiyang.sell.dataobject.ProductCategory;
import com.qiyang.sell.dataobject.ProductInfo;
import com.qiyang.sell.service.CategoryService;
import com.qiyang.sell.service.ProductService;
import com.qiyang.sell.utils.ResultVOUtil;
import com.qiyang.sell.vo.ProductInfoVo;
import com.qiyang.sell.vo.ProductVo;
import com.qiyang.sell.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created on 2018/7/1.
 *
 * @author sehuaqi.
 * 描述：
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResultVo list() {

        //1. 查询所有上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();


        //查询类目
//        List<Integer> categroyTypeList = new ArrayList<>();
        //传统方法
//        for (ProductInfo productInfo: productInfoList){
//            categroyTypeList.add(productInfo.getCategoryType());
//        }

        //2. 精简方法，JAVA8 lambda表达式
        List<Integer> categroyTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());


        List<ProductCategory> productCategories = categoryService.findByCategoryTypeIn(categroyTypeList);


        List<ProductVo> productVoList = new ArrayList<>();
        //3. 数据拼装
        for (ProductCategory productCategory : productCategories) {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryType(productCategory.getCategoryType());
            productVo.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVo> productInfoVos = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVo);
                    productInfoVos.add(productInfoVo);
                }
            }
            productVo.setProductInfoVoList(productInfoVos);
            productVoList.add(productVo);

        }


        ResultVo resultVo = new ResultVo<>();

//        ProductVo productVo = new ProductVo();
//
//        ProductInfoVo productInfoVo = new ProductInfoVo();
//
//        productVo.setProductInfoVoList(Arrays.asList(productInfoVo));

//        resultVo.setData(Arrays.asList(productVo));

//        resultVo.setData(productVoList);
//
//        resultVo.setCode(0);
//        resultVo.setMsg("成功");

        return ResultVOUtil.success(productVoList);
    }


}
